import {
    Message
} from "element-ui"
import crypto from "crypto-js";
export function getMd5(str) {
    return crypto
        .MD5(str)
        .toString()
        .toUpperCase();
}
export function showOkMsg(msg) {
    Message({
        type: "success",
        message: msg,
        duration: 2000,
    });
}
export function showErrorMsg(msg) {
    Message({
        type: "error",
        message: msg,
        duration: 2000,
    });
}

/**
 * 清除空参数
 * @param params
 */
export function clearBlankParam(params) {
    const result = {};
    Object.keys(params).forEach(key => {
        if (params[key] !== undefined && params[key] !== null && params[key] !== '') {
            result[key] = params[key]
        }
    });
    return result;
}

/**
 * 格式化日期
 * @param date Date类型
 * @param fmt 格式化模板： YYYY-MM-DD HH:mm:ss
 * @returns {*}
 */
export function formatDate(date, fmt = 'YYYY-MM-DD HH:mm:ss') {
    let t = {
        'YYYY': date.getFullYear(),
        'MM': date.getMonth() + 1,
        'DD': date.getDate(),
        'HH': date.getHours(),
        'mm': date.getMinutes(),
        'ss': date.getSeconds()
    };
    for (let item in t) {
        if (fmt.indexOf(item) >= 0) {
            fmt = fmt.replace(item, t[item] >= 10 ? t[item] : `0${t[item]}`)
        }
    }
    return fmt
}

/**
* 获取昨天日期
*/
export function yesterday() {
    let day = new Date();
    day.setTime(day.getTime() - 24 * 60 * 60 * 1000);
    return day.getFullYear() + "-" + (day.getMonth() + 1) + "-" + day.getDate();
}

/**
* 获取今天日期
*/
export function today() {
    let day = new Date();
    day.setTime(day.getTime());
    return day.getFullYear() + "-" + (day.getMonth() + 1) + "-" + day.getDate();
}

/**
 * 生成随机字符串
 * @param len 长度
 * @param strs 所有字符
 * @returns {string}
 */
export function randomStr(len, strs) {
    let allStrs = strs || 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    let maxPos = allStrs.length;
    let result = '';
    for (let i = 0; i < len; i++) {
        result += allStrs.charAt(Math.floor(Math.random() * maxPos));
    }
    return result;
}
